package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.mapper.MemberMapper;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.DateUtils;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.*;

@Service(interfaceClass = MemberService.class)
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberMapper memberMapper;

    /**
     * 通过手机号查询会员
     * @param telephone
     * @return
     */
    @Override
    public Member findByTelephone(String telephone) {

        return memberMapper.findByTelephone(telephone);
    }

    /**
     * 注册会员
     * @param member
     */
    @Override
    public void add(Member member) {

        //对密码进行加密
        if (member.getPassword()!=null){
            member.setPassword(MD5Utils.md5(member.getPassword()));
        }
    }


    /**
     * 查询过去12个月的每个月的会员数量
     * @return
     */
    @Override
    public Map getMemberReport() {

        //获取日历对象实例
        Calendar calendar = Calendar.getInstance();

        //将日历对象往前推12个月
        calendar.add(Calendar.MONTH,-12);


        //定义一个集合存储每个月的会员数量
        List<Integer> memberCounts = new ArrayList<>();

        //定义一个集合存储每个月
        List<String> months = new ArrayList<String>();

        for (int i = 0; i < 12; i++) {

            //获取当前时间
            Date time = calendar.getTime();

            //将 日期对象转换成String的年月，即2019-09
            String date = new SimpleDateFormat("yyyy-MM").format(time);

            months.add(date);

            String monthBegin = date+"-01";
            String monthEnd = date+"-31";

            //查询每个月的数据
            Integer memberCount = memberMapper.findMemberCountByMonth(monthBegin,monthEnd);
            memberCounts.add(memberCount);

            //将日历对象+1，比如：-12 、 -11 ，-10
            calendar.add(Calendar.MONTH,1);

        }

        //定义一个map，存放每个月的会员数量和每个月的月份
        Map<String, List> map = new HashMap<>();
        map.put("months",months);
        map.put("memberCounts",memberCounts);
        return map;
    }

<<<<<<< HEAD
=======
    @Override
    public List<Map> getMemberSexCount() {
        ArrayList<Map> list = new ArrayList<>();
        Map<String, Object> map1 = new HashMap<>();
        Map<String, Object> map2 = new HashMap<>();
        Integer count1 = memberMapper.getMemberMan();
        Integer count2 = memberMapper.getMemberWomen();
        map1.put("name","男");
        map1.put("value",count1);
        map2.put("name","女");
        map2.put("value",count2);
        list.add(map1);
        list.add(map2);
        return list;
    }

    @Override
    public List<Map> getCountByAge() {
        ArrayList<Map> list = new ArrayList<>();
        Integer count1 = memberMapper.getCountByAge(0,18);
        Integer count2 = memberMapper.getCountByAge(19,30);
        Integer count3 = memberMapper.getCountByAge(31,45);
        Integer count4 = memberMapper.getCountByAge(45,999);
        Map<String, Object> map1 = new HashMap<>();
        Map<String, Object> map2 = new HashMap<>();
        Map<String, Object> map3 = new HashMap<>();
        Map<String, Object> map4 = new HashMap<>();
        map1.put("name","0-18岁");
        map1.put("value",count1);
        map2.put("name","19-30岁");
        map2.put("value",count2);
        map3.put("name","31-45岁");
        map3.put("value",count3);
        map4.put("name","45岁以上");
        map4.put("value",count4);
        list.add(map1);
        list.add(map2);
        list.add(map3);
        list.add(map4);
        return list;
    }

>>>>>>> cheng
}
